<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head>
    <title>自己的电商系统</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/tpshop/Public/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/tpshop/Public/css/bootstrap-theme.min.css" />
    <link rel="stylesheet" href="/tpshop/Public/css/front.css" />
</head>
<body>
    <div class="container shopcart">
        <div class="row">
            <div class="col-md-3"><a href="<?php echo U('home//front/index') ?>"><img id="logo" src="/tpshop/Public/img/logo.jpg"></a>
            </div>
            <div class=" col-md-9">
                <img style="height:50px; margin-top:40px" class="pull-right" src="/tpshop/Public/img/shop_step_1.jpg ">
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <table id="top" class="table table-bordered table-hover">
                    <tr class="tp">
                        <th width="5%"></th>
                        <th width="30%">商品</th>
                        <th width="20%%">单价</th>
                        <th width="20%">数量</th>
                        <th width="20%">小计（元）</th>
                        <th width="5%">操作</th>
                    </tr>
                        <?php $total=0;foreach ($cart as $c ) { $prodect=M('prodect')->where('id='.$c['prodect_id'])->find(); ?>
                    <tr class="bottom">
                        <td ><input type="checkbox" value="<?=$c['id'] ?>" class='is_select'></td>
                         <input type="hidden" name="prodect_id" value="<?=$c['prodect_id'] ?>" class="prodect_id">
                        <td ><a  href="<?php echo U('prodect/detail/'.$prodect['id']) ?>"><img style="height:60px;" src="/tpshop<?php echo ($prodect['img']) ?>"><?php echo $prodect['title'] ?></a></td>
                        <td >￥<?php echo number_format($prodect['price'],2) ?></td>
                        <td ><div class="clearfix"><span class="minus">-</span><input type="text" name="num" value="<?php echo $c['num'] ?>" class="n"><span class="add">+</span></div></td>
                        <td ><?php echo $n=intval($prodect['price'])*(int)$c['num'] ?></td>
                        <td ><a href="<?php echo U("home/shoping/del/id/".$c['id']); ?>" class="del">删除</a></td>
                    </tr>     
                    <?php
 $total=$total+$n; } ?>
                </table>
            </div>
        </div>
        <hr/>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-bordered" id="total">
                    <tr>
                        <td><div class="checkbox">
                            <label>
                              <input type="checkbox" id="checkall"> 全选
                            </label>
                            </div>
                        </td>
                        <td> <div><a href="" id="del_all">全部删除</a></div></td>
                        <td>合计:￥<?php echo number_format($total,2) ?></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">     
            </div>
            <div class="col-md-6">
                <button class="btn btn-danger pull-right" id="buy">去结算</button>
            </div>
        </div>
    </div>
</body>
</html>    
    <script type="text/javascript" src="/tpshop/Public/js/jquery.min.js"></script>    
    <script type="text/javascript">
        $(window).load(function(){
            $('#checkall').click(function(){
                $('input:checkbox').prop("checked",this.checked);
            });
            $('.del').click(function(){
                if(confirm('是否删除？')){
                var href=$(this).attr('href');
                $.get(href,function(data,status){
                    location.reload(); 
                });
            }
                return false;
            });
            $('#del_all').click(function(){
                var ids=[];
                $('.is_select').each(function(i,item){
                    if($(item).prop("checked")){
                        ids.push($(item).val());
                    }
                });
                if(ids.length==0){
                    alert("没有选择任何商品");
                }else{
                    if(confirm('是否删除？')){
                    $.post("<?php echo U('home/shoping/del_all') ?>",{
                        "ids":ids},function(data){
                            if(data=="ok"){location.reload()}
                        })
                    }
                }
                return false;
            });
            $('.minus').click(function(){
               var id=$(this).parents('.bottom').find('.is_select').val();
               var num=parseInt($(this).parents('.bottom').find('.n').val())-1;
               $.post("<?=U('home/shoping/update') ?>",{'id':id,'num':num},function(data){
                if(data=="ok"){location.reload()}else{alert(data)}
               })
            });
            $('.add').click(function(){
               var id=$(this).parents('.bottom').find('.is_select').val();
               var num=parseInt($(this).parents('.bottom').find('.n').val())+1;
               $.post("<?=U('shoping/update') ?>",{'id':id,'num':num},function(data){
                if(data=="ok"){location.reload()}
               })
            });
            $('.n').blur(function(){
               var id=$(this).parents('.bottom').find('.is_select').val();
               var num=parseInt($(this).val());
               $.post("<?=U('shoping/update') ?>",{'id':id,'num':num},function(data){
                if(data=="ok"){location.reload()}else{alert(data)}
               })
            });
            $('#buy').click(function(){
                var data=[];
                $('.is_select').each(function(i,item){
                    if($(item).prop("checked")){
                    var element={
                        'prodect_id':$(this).parents('.bottom').find('.prodect_id').val(),
                        'num':$(this).parents('.bottom').find('.n').val(),
                        }
                        data.push(element);
                    }
                 });
                if(data.length==0){
                    alert('请至少选中一件商品');
                }else{
                     $.post("<?=U('home/shoping/preorder') ?>",{'data':data},function(data){
                     if(data=='ok'){
                        location.href="<?=U('home/shoping/createorder') ?>"
                        }else{
                            alert(data)
                        }
                    })
                }
                 return false;
            });
        })
    </script>